package com.app.service;

import java.util.List;

import javax.xml.bind.ValidationException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.app.dao.UserEquipmentDao;
import com.app.entity.ResidentialEquipment;
import com.app.responseVO.PageVO;
import com.app.responseVO.ReturnResultResponseVO;
import com.app.responseVO.UserResponseVO;
@Service
@Transactional
public class UserEquipmentService {

	@Autowired
	private UserEquipmentDao userEquipmentDao;
	
	public UserResponseVO insertUserEquipment(ResidentialEquipment residentialEquipment){
		UserResponseVO responseVO = new UserResponseVO();
		responseVO.setMsg("新增设备成功!");
		responseVO.setStatue(true);
		try {
			int nResult = userEquipmentDao.insertUserEquipment(residentialEquipment);
			if(nResult == 0){
				responseVO.setMsg("新增设备失败!");
				responseVO.setStatue(false);
			}
		} catch (Exception e) {
			responseVO.setMsg("新增设备失败!");
			responseVO.setStatue(false);
		}
		return responseVO;
	}
	
	public UserResponseVO updateUserEquipment(ResidentialEquipment residentialEquipment){
		UserResponseVO responseVO = new UserResponseVO();
		responseVO.setMsg("操作成功!");
		responseVO.setStatue(true);
		try {
			int nResult = userEquipmentDao.updateUserEquipment(residentialEquipment);
			if(nResult == 0){
				responseVO.setMsg("操作失败!");
				responseVO.setStatue(false);
			}
		} catch (Exception e) {
			responseVO.setMsg("操作失败!");
			responseVO.setStatue(false);
		}
		return responseVO;
	}
	public UserResponseVO deleteUserEquipment(ResidentialEquipment residentialEquipment){
		UserResponseVO responseVO = new UserResponseVO();
		responseVO.setMsg("操作成功!");
		responseVO.setStatue(true);
		try {
			int nResult = userEquipmentDao.deleteUserEquipment(residentialEquipment);
			if(nResult == 0){
				responseVO.setMsg("操作失败!");
				responseVO.setStatue(false);
			}
		} catch (Exception e) {
			responseVO.setMsg("操作失败!");
			responseVO.setStatue(false);
		}
		return responseVO;
	}
	public ReturnResultResponseVO<ResidentialEquipment>  selectUserEquipmentList(ResidentialEquipment residentialEquipment,int currentPage, int pageSize) throws ValidationException{
		
		ReturnResultResponseVO<ResidentialEquipment> responseData = new ReturnResultResponseVO<ResidentialEquipment>();
        int countPage = 0;
        int count = userEquipmentDao.selectUserEquipmentListNum(residentialEquipment);
        try {
            if (count % pageSize == 0) {
                countPage = count / pageSize;
            } else {
                countPage = count / pageSize + 1;
            }
            if (currentPage < 1) {
                currentPage = 1;
            }
            if (currentPage > countPage) {
                currentPage = countPage;
            }
            PageVO pageVO = new PageVO();
            pageVO.setCountPage(countPage);
            pageVO.setCurrentPage(currentPage);
            pageVO.setPagesize(pageSize);
            responseData.setPage(pageVO);
            List<ResidentialEquipment> oResidentialGuarantee = userEquipmentDao.selectUserEquipmentList(residentialEquipment, (currentPage - 1) * pageSize, pageSize);
            responseData.setData(oResidentialGuarantee);
        } catch (Exception e) {
            throw new ValidationException("获取列表异常");
        }
        return responseData;
	}
}
